﻿<template>
  <a-modal
    title="编辑客商信息表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="客商编号" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商编号" v-decorator="['merchantNo']" />
        </a-form-item>
        <a-form-item label="客商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商名称" v-decorator="['merchantName']" />
        </a-form-item>
        <a-form-item label="客商分类" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择客商分类" v-decorator="['merchantType', {rules: [{ required: true, message: '请选择客商分类！' }]}]">
            <a-select-option v-for="(item,index) in merchantTypeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="客商简称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商简称" v-decorator="['merchantShort']" />
        </a-form-item>
        <a-form-item label="客商等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select style="width: 100%" placeholder="请选择客商等级" v-decorator="['merchantGrade', {rules: [{ required: true, message: '请选择客商等级！' }]}]">
            <a-select-option v-for="(item,index) in merchantGradeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="客商属性" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商属性" v-decorator="['merchantAttribute']" />
        </a-form-item>
        <a-form-item label="客商行业" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商行业" v-decorator="['merchantTmt']" />
        </a-form-item>
        <a-form-item label="客商联系人" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商联系人" v-decorator="['merchantContact']" />
        </a-form-item>
        <a-form-item label="客商电话" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商电话" v-decorator="['merchantPhone']" />
        </a-form-item>
        <a-form-item label="客商传真" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商传真" v-decorator="['merchantFax']" />
        </a-form-item>
        <a-form-item label="客商邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商邮箱" v-decorator="['merchantEmail']" />
        </a-form-item>
        <a-form-item label="客商地址" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入客商地址" v-decorator="['merchantAddress']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {
    WmsMerchantinfoEdit
  } from '@/api/modular/main/WmsMerchantinfoManage'
  export default {
    data () {
      return {
	    Id: 0,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        record: {},
        merchantTypeData: [],
        merchantGradeData: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true;
		this.Id = record.id;
        this.$nextTick(() => {
        });
        //深度拷贝 移除VUE的监听，防止INDEX页面值变动
        this.record = JSON.parse(JSON.stringify(record))
        const merchantTypeOption = this.$options
        this.merchantTypeData = merchantTypeOption.filters['dictData']('MerchantType')
        const merchantGradeOption = this.$options
        this.merchantGradeData = merchantGradeOption.filters['dictData']('MerchantGrade')
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              merchantNo: record.merchantNo,
              merchantName: record.merchantName,
              merchantType: record.merchantType,
              merchantShort: record.merchantShort,
              merchantGrade: record.merchantGrade,
              merchantAttribute: record.merchantAttribute,
              merchantTmt: record.merchantTmt,
              merchantContact: record.merchantContact,
              merchantPhone: record.merchantPhone,
              merchantFax: record.merchantFax,
              merchantEmail: record.merchantEmail,
              merchantAddress: record.merchantAddress
            }
          )
        })
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (values[key] == null) continue
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
                 this.record[key] = values[key]
              } else {
                 this.record[key] = values[key]
              }
            }
            WmsMerchantinfoEdit(this.record).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', this.record)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          }else{
            this.confirmLoading = false
          } 
        });
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
